package tbdp.tool.config.loader;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;

import tbdp.tool.config.JobConfig;
import tbdp.tool.util.GsonUtils;
import tbdp.tool.util.ResourceUtil;

/**
 * @author xbkaishui
 * @version $Id: ConfigLoadUtil.java, v 0.1 2017-06-17 下午4:03 xbkaishui Exp $$
 */
public class ConfigLoadUtil {

    private static final Logger logger = LoggerFactory.getLogger(ConfigLoadUtil.class);

    private static final String suffix = ".conf";

    public static JobConfig loadConfig(String configName) {
        logger.info("load config " + configName);
        try {
            InputStream ins = ResourceUtil.getInputStream(configName + suffix);
            String content = IOUtils.toString(ins, "UTF-8");
            JobConfig config = GsonUtils.fromJson(content, JobConfig.class);
            return config;
        } catch (Exception e) {
            throw new RuntimeException("load job error!", e);
        }
    }
}
